package com.fpx.metadata.service.handle;


import com.fpx.metadata.bean.po.DbInfo;
import com.fpx.metadata.bean.po.RJob;
import com.fpx.metadata.bean.po.RTransformation;
import com.fpx.metadata.service.RJobService;
import com.fpx.metadata.service.RTransformationService;
import com.fpx.metadata.tool.DynamicDataSourceManager;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j2
public class KettleMetaDataHandle {

    @Autowired
    private DynamicDataSourceManager dynamicDataSourceManager;
    @Autowired
    private RJobService rJobService;
    @Autowired
    private RTransformationService rTransformationService;

    @Async
    public void buildMetaDataInfo(DbInfo dbInfo) throws Exception {
        //1.获取数据源
        JdbcTemplate jdbcTemplate = dynamicDataSourceManager.getDataSourcePoolByDatabase(dbInfo);
        //3.保存job信息
        List<RJob> rJobs = jdbcTemplate.query("select * from R_JOB", rs -> {
            List<RJob> jobListResult = Lists.newArrayList();
            while (rs.next()) {
                RJob rJob = new RJob();
                rJob.setIdJob(rs.getLong("ID_JOB"));
                Integer idDirectory = rs.getInt("ID_DIRECTORY");
                rJob.setDirectory(buildDirectory(jdbcTemplate, idDirectory));
                rJob.setName(rs.getString("NAME"));
                rJob.setJobVersion(rs.getString("JOB_VERSION"));
                rJob.setJobStatus(rs.getInt("JOB_STATUS"));
                rJob.setCreatedUser(rs.getString("CREATED_USER"));
                rJob.setCreatedDate(rs.getDate("CREATED_DATE"));
                rJob.setModifiedUser(rs.getString("MODIFIED_USER"));
                rJob.setModifiedDate(rs.getDate("MODIFIED_DATE"));
                rJob.setDescription(rs.getString("DESCRIPTION"));
                rJob.setExtendedDescription(rs.getString("EXTENDED_DESCRIPTION"));
                jobListResult.add(rJob);
            }
            return jobListResult;
        });
        assert rJobs != null;
        for (RJob rJob : rJobs) {
            assert rJobService.saveSelect(rJob) > 0;
        }
        log.info("job infos：{}", rJobs);
        //4.保存转换信息
        List<RTransformation> rTransformations = jdbcTemplate.query("select * from R_TRANSFORMATION", rs -> {
            List<RTransformation> rTransformationListRs = Lists.newArrayList();
            while (rs.next()) {
                RTransformation rTransformation = new RTransformation();
                rTransformation.setIdTransformation(rs.getLong("ID_TRANSFORMATION"));
                rTransformation.setDirectory(buildDirectory(jdbcTemplate, rs.getInt("ID_DIRECTORY")));
                rTransformation.setName(rs.getString("NAME"));
                rTransformation.setTransVersion(rs.getString("TRANS_VERSION"));
                rTransformation.setTransStatus(rs.getInt("TRANS_STATUS"));
                rTransformation.setCreatedUser(rs.getString("CREATED_USER"));
                rTransformation.setCreatedDate(rs.getDate("CREATED_DATE"));
                rTransformation.setModifiedUser(rs.getString("MODIFIED_USER"));
                rTransformation.setModifiedDate(rs.getDate("MODIFIED_DATE"));
                rTransformation.setDescription(rs.getString("DESCRIPTION"));
                rTransformation.setExtendedDescription(rs.getString("EXTENDED_DESCRIPTION"));
                rTransformationListRs.add(rTransformation);
            }
            return rTransformationListRs;
        });
        assert rTransformations != null;
        for (RTransformation rTransformation : rTransformations) {
            assert rTransformationService.saveSelect(rTransformation) > 0;
        }
        log.info("转换信息：{}", rTransformations);
    }

    private String buildDirectory(JdbcTemplate jdbcTemplate, Integer idDirectory) {
        StringBuilder sb = new StringBuilder();
        while (idDirectory != null && idDirectory != 0) {
            idDirectory = jdbcTemplate.query("select * from R_DIRECTORY WHERE id_directory = ?", new Object[]{idDirectory}, rs1 -> {
                int idPirectoryParent = 0;
                if (rs1.next()) {
                    idPirectoryParent = rs1.getInt("ID_DIRECTORY_PARENT");
                    sb.insert(0, rs1.getString("DIRECTORY_NAME"));
                    sb.insert(0, rs1.getString("/"));
                }
                return idPirectoryParent;
            });
        }
        return sb.append("/").toString();
    }
}
